<template>
  <div class="table-wrap">
    <div class="search_header">
      <span>
        <el-button
          icon="el-icon-download"
          @click="downloadProject"
          :loading="btnLoading"
          v-has="'patrolRecord:export'"
          >导出巡查记录</el-button
        >
      </span>
      <div class="search_header_right">
        <el-date-picker
          placeholder="巡查时间"
          v-model="parameter.yearMonth"
          type="month"
          value-format="yyyy-MM"
          @change="search"
        ></el-date-picker>
        <el-select
          placeholder="整改情况"
          v-model="parameter.changeStatus"
          clearable
          @change="search"
        >
          <el-option
            v-for="item in changeStatusOptions"
            :key="item.dictValue"
            :label="item.dictText"
            :value="item.dictValue"
          >
          </el-option>
        </el-select>
        <el-input
          placeholder="请输入项目名称、项目编号、建设单位"
          v-model="parameter.keywords"
          @keydown.enter.native="search"
          clearable
        >
          <span
            @click="search"
            slot="suffix"
            style="display: inline-block"
          >
            <Icon
              title="搜索"
              name="sousuo"
              color="#A7ADB5"
              font-size="20"
            />
          </span>
        </el-input>
      </div>
    </div>
    <el-table
      :data="projectList"
      :header-cell-style="{
        background: '#F2F6FB ',
        color: ' #6B767D'
      }"
      :row-class-name="tableRowClassName"
      v-loading="loading"
    >
      <el-table-column
        label="序号"
        align="center"
        show-overflow-tooltip
        width="80"
      >
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        label="项目编号"
        prop="projectNo"
        show-overflow-tooltip
      />
      <el-table-column
        label="项目名称"
        prop="projectName"
        show-overflow-tooltip
      />
      <el-table-column
        label="标段名称"
        prop="sectionName"
        show-overflow-tooltip
      />
      <el-table-column
        label="施工单位"
        prop="sgdw"
        show-overflow-tooltip
      />
      <el-table-column
        label="巡查日期"
        prop="inspectionTime"
        show-overflow-tooltip
      >
        <template slot-scope="scope">{{
          scope.row.inspectionTime.substring(0, 10)
        }}</template>
      </el-table-column>
      <el-table-column
        label="存在问题数"
        prop="problemCount"
        show-overflow-tooltip
      />
      <el-table-column
        label="整改情况"
        prop="changeStatusDictText"
        show-overflow-tooltip
      />
      <el-table-column
        label="更新时间"
        prop="updateTime"
        show-overflow-tooltip
      />
      <el-table-column
        label="操作"
        show-overflow-tooltip
        width="180"
      >
        <template slot-scope="scope">
          <el-button
            v-if="
              showEdit(scope.row) &&
              $filterGlobalPermission('patrolRecord:change')
            "
            @click="updateProgress(scope.row)"
            type="text"
            style="color: #00baff; font-weight: 400"
            >整改</el-button
          >
          <el-button
            v-if="
              showBack(scope.row) &&
              $filterGlobalPermission('patrolRecord:back')
            "
            @click="backProject(scope.row)"
            type="text"
            style="color: #ff5b5b; font-weight: 400"
            >退回
          </el-button>
          <el-button
            @click="viewProject(scope.row)"
            type="text"
            style="color: #00baff; font-weight: 400"
            >查看</el-button
          >
          <el-button
            v-if="
              showDetete(scope.row) &&
              $filterGlobalPermission('patrolRecord:delete')
            "
            @click="deleteProject(scope.row)"
            type="text"
            style="color: #ff5b5b; font-weight: 400"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="table-pagination" v-if="total">
      <el-pagination
        background
        layout="prev, pager, next"
        :page-size="parameter.pageSize"
        @current-change="pageChange"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {
  backProjectInfo,
  queryProjectList,
  deleteProjectInfo,
  projectFileDownload
} from '@/api/patrolRecord'
import {
  isAdmin,
  isEnterprise,
  isJldw,
  isSgdw,
  isJcdw
} from '@/utils/permission'
import { getDictOptions } from '@/utils/dict'
import Icon from '@/components/icon'

export default {
  name: 'index',
  components: {
    Icon
  },
  data() {
    return {
      projectList: [],
      total: '',
      parameter: {
        yearMonth: '',
        keywords: '',
        changeStatus: '',
        pageIndex: 1,
        pageSize: 10
      },
      changeStatusOptions: [],
      loading: false,
      btnLoading: false
    }
  },
  mounted() {
    this.initOptions()
    this.getList()
  },
  methods: {
    /**
     * 整改按钮显示：
     * 施工单位 && 待整改状态
     * 管理员 && 待整改状态
     */
    showEdit(row) {
      if (row.changeStatus === '0') {
        return true
      } else {
        return false
      }
      // if (isSgdw() && row.changeStatus === '0') {
      //   return true
      // } else if (isAdmin() && row.changeStatus === '0') {
      //   return true
      // } else {
      //   return false
      // }
    },
    /**
     * 退回按钮显示：
     * 管理员 && 已整改状态 => 待整改
     * 管理员 && 待整改状态 => 待提交
     */
    showBack(row) {
      if (row.changeStatus === '1') {
        return true
      } else if (row.changeStatus === '0') {
        return true
      } else {
        return false
      }
      // if (isAdmin() && row.changeStatus === '1') {
      //   return true
      // } else if (isAdmin() && row.changeStatus === '0') {
      //   return true
      // } else {
      //   return false
      // }
    },
    /**
     * 删除按钮显示：
     * 管理员
     */
    showDetete(row) {
      return true
      // if (isAdmin()) {
      //   return true
      // } else {
      //   return false
      // }
    },
    async initOptions() {
      const data = await getDictOptions('zhjs_zgzt')
      this.changeStatusOptions = data
    },

    search() {
      this.parameter.pageIndex = 1
      this.getList()
    },

    pageChange(page) {
      this.parameter.pageIndex = page
      this.getList()
    },

    // 获取用户列表
    getList() {
      this.loading = true
      queryProjectList(this.parameter).then((res) => {
        if (res.success) {
          this.total = res.data.count
          this.projectList = res.data.list
        }
        this.loading = false
      })
    },

    // 表格高亮
    tableRowClassName({ rowIndex }) {
      if (rowIndex % 2 === 1) {
        return 'light'
      } else {
        return ''
      }
    },

    downloadProject() {
      this.btnLoading = true
      projectFileDownload(this.parameter).then((res) => {
        const blob = new Blob([res])
        const a = document.createElement('a')
        a.href = URL.createObjectURL(blob)
        a.download = '巡查记录.xlsx'
        a.style.display = 'none'
        document.body.appendChild(a)
        a.click()
        a.remove()
        this.btnLoading = false
      })
    },

    // 进度更新
    updateProgress(row) {
      this.$router.push({
        path: '/wisdomConstruction/projectPatrolManagement/patrolRecord/add',
        query: {
          pkid: row.pkid,
          title: '整改',
          isEdit: true,
          problemCount: row.problemCount
        }
      })
    },
    // 项目查看
    viewProject(row) {
      this.$router.push({
        path: '/wisdomConstruction/projectPatrolManagement/patrolRecord/add',
        query: {
          pkid: row.pkid,
          title: '查看',
          isEdit: true,
          isDetail: true,
          problemCount: row.problemCount
        }
      })
    },

    // 删除项目
    deleteProject(row) {
      this.$confirm(
        '此操作将永久删除该巡查记录，是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          deleteProjectInfo(row.pkid).then((res) => {
            if (res.success) {
              this.$message.success('删除成功')
              if (
                this.total % 10 === 1 &&
                this.parameter.pageIndex ===
                  parseInt(this.total / 10) + 1 &&
                this.parameter.pageIndex !== 1
              ) {
                this.parameter.pageIndex -= 1
              }
              this.getList()
            } else {
              this.$message.error(res.message)
            }
          })
        })
        .catch((error) => {
          this.$message.info('已取消删除')
        })
    },

    // 退回项目
    backProject(row) {
      this.$confirm(
        '此操作将退回该巡查记录，是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          backProjectInfo({
            pkid: row.pkid,
            changeStatus:
              row.changeStatus === '1' ? '0' : '-1'
          }).then((res) => {
            if (res.success) {
              this.$message.success('退回成功')
              this.getList()
            } else {
              this.$message.error(res.message)
            }
          })
        })
        .catch((error) => {
          this.$message.info('已取消退回')
        })
    }
  }
}
</script>

<style scoped lang="scss">
@import '~@/assets/css/table.scss';
</style>
